package com.eascs.admin.o2o.loan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.loan.action.O2oLoanUserActionForm;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.app.dao.PageResult;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.MessageRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;

@Controller
@RequestMapping("/loanUser")
public class O2oLoanUserController extends BaseMasterViewController {
	@Reference
	ICustomerInfoService iCustomerInfoService;
	@Value("${o2o.admin.findUserUrl}")
	String url;
	@Value("${o2o.admin.editUserUrl}")
	String editUserUrl;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "loanManage");
	}

	/**
	 * 查询用户
	 * 
	 * @return
	 */
	@RequestMapping(value = "/finduser")
	public String index() {
		return "redirect:" + url;
	}

	/**
	 * 编辑用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/edituser/{userId}")
	public String editUser(@PathVariable("userId") String userId) {
		return "redirect:" + editUserUrl + userId;
	}

	/**
	 * 跳转到添加借款人页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add")
	public String addLoan() {
		return "/loanManagement/addLoanUser";
	}


	@RequestMapping(value = "/reloan")
	public BaseRestResult<String> reLoan() {
		return MessageRestResult.redirect("/loanUser");
	}

	public String addUserResult(String massege, O2oLoanUserActionForm o2oLoanUserActionForm) {
		ViewContext.put("o2oLoanUserActionForm", o2oLoanUserActionForm);
		ViewContext.put("massege", massege);
		return "/loanManagement/addLoanUser";
	}
	
	/**
	 * 担保人上传附件
	 * @param loanapplyNo
	 * @param customerId
	 * @return
	 *//*
	@RequestMapping(value = "/findsponsorfile")
	public String sponsorfile(String loanapplyNo, String customerId) {
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyNo);
		*//**** 查询附件 ****//*
		List<O2oDataListEntity> o2oDataList = o2oDataListService.findByEntityList(loanapplyNo);
		QueryCustomerVO vo = new QueryCustomerVO();
		vo.setCustomer_id(customerId);
		CustomerView customer = iCustomerInfoService.geCustomerView(vo);
		ViewContext.put("basic", customer.getBasicInfo());
		ViewContext.put("o2oDataList", o2oDataList);
		ViewContext.put("loanApplyFileEntity", o2oLoanapplyEntity);
		return "loanManagement/sponsorfile";
	}*/
	
	@RequestMapping(value = "/findusername")
	public String findusername(String username) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "loanUserfindusername");
		ViewContext.put("paginationInfo", null);
		return "/loanManagement/findusername";
	}
	@RequestMapping(value = "/findusernameQuery")
	public String findusernameQuery(String username) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "loanUserfindusername");
		if (com.eascs.admin.common.utils.StringUtils.isEmpty(username)) {
			ViewContext.put("paginationInfo", null);
		}else {
			PageResult<Object[]> pageResult = o2oLoanUserService.findByUserName(username);
			String titles[] = {"客户姓名","身份证号", "手机号", "信贷经理","省区"};
//			Button[] titleButtons = new Button[] {null,ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
			PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
//			paginationInfo.setTitleButtons(titleButtons);
			paginationInfo.setBuildPagingNavigation(false);
			paginationInfo.setFirstRenderColumnIndex(2);
			paginationInfo.setBuildRowNumber(true);
			ViewContext.put("username", username);
			ViewContext.put("paginationInfo", paginationInfo);
		}
		return "loanManagement/findusername";
	}
}
